﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SqlServerAdmin;
using IQ.BaseWindows;

namespace IQ.DbA
{
    public partial class FrmSyncObjText : Form
    {
        #region # Constructor #
        public FrmSyncObjText()
        {
            InitializeComponent();
            grdFileName.AutoGenerateColumns = false;
            grdServerName.AutoGenerateColumns = false;
            grdChanges.AutoGenerateColumns = false;
        }
		#endregion
		
		#region - Private variables - 
        private SqlMdText sqlMD = new SqlMdText();
        private bool checkExcludes = false;
        #endregion 

        #region - Protected variables - 
        protected string objectType = SqlQueries.SoT_StoreProc;
        #endregion

		#region - Private methods - 
        private void loadCompareList()
        {
            Dictionary<string,string> list = new Dictionary<string,string>();
            list.Add(SqlQueries.SoT_Functions, "Functions");
            list.Add(SqlQueries.SoT_FunctionsAsm, "Functions Sys.");
            list.Add(SqlQueries.SoT_StoreProc, "Store procedures");
            list.Add(SqlQueries.SoT_View,"Views");
            cbxCompareList.Items.Clear();
            DataBind.BindComboBoxOptions(cbxCompareList, list);
        }
        private void loadScreenForType()
        {
            objectType = cbxCompareList.SelectedValue.ToString();
            grdFileName.DataSource = sqlMD.FileStoreProc;
            grdServerName.DataSource = sqlMD.ServerSp;
            grdChanges.DataSource = sqlMD.Changes;
        }
        private void setMaxProgress(int value)
        {
            pgbAll.Maximum = value;
        }
        private void setProgress(int value)
        {
            pgbAll.Value = value;
        }
        private void compareObjectText()
        {
            if (grdChanges.CurrentRow != null)
            {
                int lenFile = 0;
                int lenServer = 0;
                txtFile.Text = sqlMD.GetFileObjectText(objectType, grdChanges.CurrentRow.Cells["ccName"].Value.ToString());
                txtServer.Text = sqlMD.GetServerObjectText(objectType, grdChanges.CurrentRow.Cells["ccName"].Value.ToString());
                for (int i = 0; i < txtFile.Lines.Count(); ++i)
                {
                    if (i < txtServer.Lines.Length)
                    {
                        if (txtFile.Lines[i] != txtServer.Lines[i])
                        {
                            txtFile.Select(lenFile, txtFile.Lines[i].Length);
                            txtFile.SelectionBackColor = Color.Yellow;
                            //
                            txtServer.Select(lenServer, txtServer.Lines[i].Length);
                            txtServer.SelectionBackColor = Color.Yellow;
                        }
                        lenFile += txtFile.Lines[i].Length + 1;
                        lenServer += txtServer.Lines[i].Length + 1;
                    }
                    else
                    {
                        txtFile.Select(lenFile, txtFile.Text.Length);
                        txtFile.SelectionBackColor = Color.Yellow;
                        i = txtFile.Lines.Count();
                    }
                }
            }
            else
                General.MesDlg("Please select a store proc to compare.", 'I');
        }
        private void checkExcludeList()
        {
            if (checkExcludes)
            {
                sqlMD.SaveExludeList(txtExclude.Lines);
                checkExcludes = false;
            }
        }
        #endregion 
		
		//#region - Public properties / variables - 
		//#endregion

		//#region - Public methods - 
		//#endregion
		
		//#region - Bind Methods - 
		//#endregion
		
		#region - Form Events -
		// This region is for event handler methods that are called without the user knowing about it.
		// Such as the page loading.
        private void FrmSyncObjText_Shown(object sender, EventArgs e)
        {
            // Check for design time
            if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)
            {
                if (SqlMdFileData.CheckCompareFileExists())
                {
                    sqlMD.SetMaxPosition += setMaxProgress;
                    sqlMD.SetCurrentPosition += setProgress;
                    txtExclude.Lines = sqlMD.Exclusions;
                    checkExcludes = false;
                    //loadScreenForType();
                }
                else
                {
                    General.MesDlg("Sql meta data file does not exists, please export meta data and then return.", 'E');
                    this.Close();
                }
            }
            loadCompareList();
            cbxCompareList.SelectedValue = SqlQueries.SoT_StoreProc;
        }
        private void frmSync_FormClosing(object sender, FormClosingEventArgs e)
        {
            checkExcludeList();
        }
        private void txtExclude_TextChanged(object sender, EventArgs e)
        {
            checkExcludes = true;
        }        
        #endregion 
		
		#region - Control Methods -
		// This region is for event handler methods that are called knowingly by the user.
		// Such as a button being clicked.
        private void btnUpdServer_Click(object sender, EventArgs e)
        {
            if (grdChanges.CurrentRow != null)
            {
                if (General.MesDlg("Update server store proc: " + grdChanges.CurrentRow.Cells[ccName.Index].Value.ToString() + " ?", 'C')
                    == DialogResult.Yes)
                    sqlMD.UpdateServerObjectText(objectType, grdChanges.CurrentRow.Cells[ccName.Index].Value.ToString());
            }
            else
                General.MesDlg("Please select a changed item to update.", 'I');
        }
        private void btnCompare_Click(object sender, EventArgs e)
        {
            compareObjectText();
        }
        private void btnCompareAll_Click(object sender, EventArgs e)
        {
            checkExcludeList();
            sqlMD.CompareAll_Sp();
        }
        private void grdChanges_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            compareObjectText();
        }
        private void grdFileData_DoubleClick(object sender, EventArgs e)
        {
            if (grdFileName.CurrentRow != null)
            {
                txtFile.Text = sqlMD.GetFileObjectText(objectType, grdFileName.CurrentRow.Cells["cfName"].Value.ToString());
            }
        }
        private void grdServerTbls_DoubleClick(object sender, EventArgs e)
        {
            if (grdServerName.CurrentRow != null)
            {
                txtServer.Text = sqlMD.GetServerObjectText(objectType, grdServerName.CurrentRow.Cells["csName"].Value.ToString());
            }
        }
        #endregion

        private void cbxCompareList_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadScreenForType();
        }
        private void btnDelServer_Click(object sender, EventArgs e)
        {
            if (grdChanges.CurrentRow != null)
            {
                if (General.MesDlg("Delete server store proc: " + grdChanges.CurrentRow.Cells[ccName.Index].Value.ToString() + " ?", 'C')
                    == DialogResult.Yes)
                    sqlMD.DeleteServerText(objectType, grdChanges.CurrentRow.Cells[ccName.Index].Value.ToString());
            }
            else
                General.MesDlg("Please select a changed item to update.", 'I');
        }

    }
}
